<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="应用名称" prop="appName">
        <el-input
          v-model="queryParams.appName"
          placeholder="请输入应用名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="应用状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择应用状态" clearable>
          <el-option
            v-for="dict in dict.type.status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:application:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:application:remove']"
        >删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="applicationList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="应用ID" align="center" prop="appId" />
      <el-table-column label="应用名称" align="center" prop="appName" />
      <el-table-column label="应用状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="更新时间" align="center" prop="updateTime" width="180" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width"  width="230">
        <template slot-scope="scope">
          <el-button
            style="padding-left: 12.7px;"
            size="mini"
            type="text"
            icon="el-icon-s-order"
            @click="uOrder(scope.row)"
            v-hasPermi="['system:application:list']"
          >订单</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-s-data"
            @click="statistics(scope.row)"
            v-hasPermi="['system:application:list']"
          >统计</el-button>
          <el-button
            v-show="scope.row.status == 1"
            size="mini"
            type="text"
            icon="el-icon-circle-check"
            @click="handleStatus(scope.row.id,2)"
            v-hasPermi="['system:application:edit']"
          >启用</el-button>
          <el-button
            v-show="scope.row.status == 2"
            size="mini"
            type="text"
            icon="el-icon-circle-close"
            @click="handleStatus(scope.row.id,1)"
            v-hasPermi="['system:application:edit']"
          >停用</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:application:edit']"
          >配置</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

  </div>
</template>

<script>
import { listApplication, getApplication, delApplication, updateApplication } from "@/api/system/application";
import {getUserOrder} from "@/api/system/order";
import { getGood } from "@/api/system/good";

export default {
  name: "Application",
  dicts: ['status'],
  data() {
    return {
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 应用管理表格数据
      applicationList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        appId: null,
        appName: null,
        signKey: null,
        payMethod: null,
        returnUrl: null,
        notifyUrl: null,
        refundUrl: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询应用管理列表 */
    getList() {
      this.loading = true;
      listApplication(this.queryParams).then(response => {
        this.applicationList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userId: null,
        appId: null,
        appName: null,
        signKey: null,
        payMethod: null,
        returnUrl: null,
        notifyUrl: null,
        refundUrl: null,
        status: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      //查询判断是否已经可以新增应用
      getUserOrder().then(response => {
        if(response != null && response != ""){
          if(response.appNum > this.applicationList.length){
            this.$store.dispatch("tagsView/delView", this.$route);
            this.$router.push({ path: '/system/application/add', query: {} })
          }else{
            this.$modal.msgError("创建应用失败，您当前应用数已达到上限！");
          }
        }else{
          this.$modal.msgError("创建应用失败，您当前还未订购套餐！");
        }
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      const id = row.id || this.ids
      this.$store.dispatch("tagsView/delView", this.$route); //关闭当前页面
      this.$router.push({ name: 'ApplicationEdit', params: {id:id} })
    },
    uOrder(row){
      const id = row.appId
      this.$store.dispatch("tagsView/delView", this.$route); //关闭当前页面
      this.$router.push({ name: 'Uorder', params: {id:id} })
    },
    statistics(row){
      const id = row.id
      this.$store.dispatch("tagsView/delView", this.$route); //关闭当前页面
      this.$router.push({ name: 'Statistics', params: {id:id} })
    },
    handleStatus(id,status){
      this.form.id = id;
      this.form.status = status;
      updateApplication(this.form).then(response => {
        if(status == 2){
          this.$modal.msgSuccess("启用成功");
        }else{
          this.$modal.msgSuccess("停用成功");
        }
        this.getList();
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除应用管理编号为"' + ids + '"的数据项？').then(function() {
        return delApplication(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/application/export', {
        ...this.queryParams
      }, `application_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
